﻿using Coldairarrow.Business.BTB_StaffUsers;
using Coldairarrow.Entity.BTB_StaffUsers;
using Coldairarrow.Entity.UserDTO;
using Coldairarrow.Util;
using Coldairarrow.Util.lib;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using tencentyun;

namespace Coldairarrow.Api.Controllers.BTB_StaffUsers
{
    [Route("/BTB_StaffUsers/[controller]/[action]")]
    public class BTB_StaffUserController : BaseApiController
    {
        #region DI

        public BTB_StaffUserController(IBTB_StaffUserBusiness bTB_StaffUserBus)
        {
            _bTB_StaffUserBus = bTB_StaffUserBus;
        }

        IBTB_StaffUserBusiness _bTB_StaffUserBus { get; }

        #endregion

        #region 获取

        [HttpPost]
        public async Task<PageResult<BTB_StaffUser>> GetDataList(PageInput<ConditionDTO> input)
        {
            return await _bTB_StaffUserBus.GetDataListAsync(input);
        }

        #endregion

        #region 提交
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        [NoCheckJWT]
        public async Task AddDataAsync(BTB_StaffUser data)
        {
            await _bTB_StaffUserBus.AddDataAsync(data);
        }

        /// <summary>
        /// 小程序——保存用户经纬度信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task SaveSiteInfo(SetSiteDTO input)
        {
            await _bTB_StaffUserBus.SaveSiteInfoAsnyc(input);
        }


        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        [NoCheckJWT]
        public async Task UserForgetPassword(UserForgetPassword data)
        {
           await _bTB_StaffUserBus.ForgetPasswordAsync(data);
        }

        /// <summary>
        /// 后台——冻结/解冻用户账号
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task ChangesUserState(IdInputDTO input)
        {
            await _bTB_StaffUserBus.ChangeUserState(input);
        }

        [HttpPost]
        public async Task DeleteData(List<string> ids)
        {
            await _bTB_StaffUserBus.DeleteDataAsync(ids);
        }

        /// <summary>
        /// 后台——获取注册用户列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<BTB_StaffUser>> GetBackStaffUserList(PageInput<ConditionDTO> input)
        {
           return await _bTB_StaffUserBus.GetBackStaffUserListAsync(input);
        }


        /// <summary>
        /// 后台——修改用户邀请码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        #endregion

        [HttpPost]
        public async Task UpdateUserYQCode(ChangeCodeDTO input)
        {
            await _bTB_StaffUserBus.UpdateYQCode(input);
        }

        /// <summary>
        /// 账号密码登录(获取token)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [NoCheckJWT]
        public async Task<string> PasswordLogin([FromBody]LoginInputDTO input)
        {
            string data = "";
          
            data = await _bTB_StaffUserBus.PasswordLogin(input);

            return data;
        }


        #region//微信H5公众号授权登录

        /// <summary>
        /// OpenId验证登录(获取token),未注册返回code-201
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [NoCheckJWT]
        public async Task<string> OpenIdLogin(OpenIdInputDTO input)
        {
            string data = "";

            data = await _bTB_StaffUserBus.OpenIdLogin(input);

            return data;
        }

        /// <summary>
        /// OpenId手机号绑定验证登录(获取token),未注册返回code-201
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [NoCheckJWT]
        public async Task<string> OPhoneLogin(OPhoneInputDTO input)
        {
            string data = "";

            data = await _bTB_StaffUserBus.OPhoneLogin(input);

            return data;
        }

        /// <summary>
        /// OpenId登录/注册(获取token)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [NoCheckJWT]
        public async Task<string> OpenIdRLogin(OpenIdRegistInputDTO input)
        {
            string data = "";

            data = await _bTB_StaffUserBus.OpenIdRegistLogin(input);

            return data;
        }

        #endregion

        #region//微信小程序授权登录

        /// <summary>
        /// OpenId验证登录(获取token),未注册返回code-201
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [NoCheckJWT]
        public async Task<string> AppletOpenIdLogin(OpenIdInputDTO input)
        {
            string data = "";

            data = await _bTB_StaffUserBus.AppletOpenIdsLogin(input);

            return data;
        }

        /// <summary>
        /// OpenId手机号绑定验证登录(获取token),未注册返回code-201
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [NoCheckJWT]
        public async Task<string> AppletOPhoneLogin(OPhoneInputDTO input)
        {
            string data = "";

            data = await _bTB_StaffUserBus.AppletOPhoneLogin(input);

            return data;
        }

        /// <summary>
        /// OpenId登录/注册(获取token)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [NoCheckJWT]
        public async Task<string> AppletOpenIdRLogin(OpenIdRegistInputDTO input)
        {
            string data = "";

            data = await _bTB_StaffUserBus.AppletOpenIdRegistLogin(input);

            return data;
        }

        #endregion

        /// <summary>
        /// 验证码登录(获取token)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [NoCheckJWT]
        public async Task<string> VerificationLogin(LoginInputDTO2 input)
        {
            string data = "";
        
            data = await _bTB_StaffUserBus.VerificationLogin(input);

            return data;
        }

        /// <summary>
        /// 验证码
        /// </summary>
        /// <param name = "input" >手机号</param >
        /// <returns></returns >
        [HttpPost]
        [NoCheckJWT]
        public IActionResult ReturnCode([FromBody]PhoneInput input)
        {
            Random random = new Random();
            string code = random.Next(100000, 999999).ToString();
            string ff = "";
            bool IsCode = Util.Helper.SMSVerification.SendSms(input.Phone, code, out ff);
            var res = new
            {
                msg = "验证码发送成功",
                success = true,
                errorCode = 0,
                data = code.ToMD5String()
            };

            if (!IsCode)
            {
                if (ff.IsNullOrEmpty())
                {
                    throw new BusException("验证码发送失败，请稍后再试", errorCode: -1);
                }
                else
                {
                    throw new BusException(ff, errorCode: -2);
                }     
            }
            return JsonContent(res.ToJson());
        }

        public class PhoneInput
        { 
            public string Phone { get; set; }
        }

        #region 获取微信登陆openid和session_key
        /// <summary>
        /// 小程序获取微信登陆openid
        /// </summary>
        /// <param name="user">用户注册后的id，用于换取openid的code码</param>
        /// <returns></returns>
        [HttpPost]
        [NoCheckJWT]
        public string GetXCXOpenId(User user)
        {
            return  _bTB_StaffUserBus.GetOpenId(user);
        }
        #endregion

        #region 获取微信登陆openid和session_key
        /// <summary>
        ///  公众号获取微信登陆openid
        /// </summary>
        /// <param name="user">用户注册后的id，用于换取openid的code码</param>
        /// <returns></returns>
        [HttpPost]
        [NoCheckJWT]
        public async Task<string> GetGZHOpenId(User user)
        {
            return await _bTB_StaffUserBus.SaveGZHData(user);
        }

        #endregion
    }
}